package net.wanho.shop.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import net.wanho.shop.dto.LoginLogDTO;
import net.wanho.shop.mapper.LoginLogMapper;
import net.wanho.shop.po.LoginLog;
import net.wanho.shop.service.LoginLogService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;


/**
 * <p>
 * 系统登录日志 服务实现类
 * </p>
 *
 * @author 猛虎王
 * @since 2024-12-24
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class LoginLogServiceImpl implements LoginLogService {

    private final LoginLogMapper loginLogMapper;

    /**
     * 系统登录日志分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo findPage(int pageNum, int pageSize, LoginLogDTO loginLogDTO) {
//    public PageInfo findPage(int pageNum, int pageSize, LoginLog loginLog) {

        // 分页
        PageHelper.startPage(pageNum, pageSize);
        List<LoginLog> loginLogList = loginLogMapper.select(loginLogDTO);
        return new PageInfo(loginLogList);
    }

    /**
     * 根据主键查询系统登录日志
     *
     * @param infoId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public LoginLog findById(Serializable infoId) {
        return loginLogMapper.selectById(infoId);
    }

    /**
     * 系统登录日志新增
     *
     * @param loginLog
     */
    @Override
    @Async
    public void add(LoginLog loginLog) {
        loginLogMapper.insert(loginLog);
    }

    /**
     * 修改系统登录日志
     *
     * @param loginLog
     */
    @Override
    public void modify(LoginLog loginLog) {
        loginLogMapper.update(loginLog);
    }

    /**
     * 根据主键删除系统登录日志
     *
     * @param infoId
     */
    @Override
    public void removeById(Serializable infoId) {
        if (((String) infoId).contains(",")) {
            loginLogMapper.deleteByIds(((String) infoId).split(","));
        } else {
            loginLogMapper.deleteById(infoId);
        }
    }
}
